home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / vla / stars / stars.asm < prev    next >
Assembly Source File  |  1993-03-15  |  10KB  |  321 lines

  1. ─────────────────────────────────────────────────────────────────────────────
  2. ;
  3. ;     TITLE: Star field
  4. ;WRITTEN BY: DRAEDEN
  5. ;      DATE: 03/15/93
  6. ;
  7. ;     NOTES: Need 386 to execute.
  8. ;
  9. ;ASSOCIATED FILES:
  10. ;
  11. ;       STARGEN.BAS =>  Basic program that generates a set of 'randomized' 
  12. ;                       numbers.  Creates STARRND.DW
  13. ;
  14. ;       STARS.TXT   =>  The text file that explains starfields...
  15. ;
  16. ;       STARRND.DW  =>  File that contains a set of shuffled numbers.  
  17. ;                       Used to create 'random' star field.
  18. ;
  19. ────────────────────────────────────────────────────────────────────────────
  20.     
  21.     DOSSEG
  22.     .MODEL SMALL
  23.     .STACK 200h
  24.     .CODE
  25.     .386
  26.     ASSUME CS:@CODE, DS:@CODE
  27.     LOCALS
  28.  
  29. ;=== GLOBALS
  30. ;=== Data Includes
  31.  
  32. INCLUDE starrnd.dw      ;file that has label StarRnd numbers 
  33.  
  34. ;=== DATA Structures
  35.     
  36.     Star_Struc      STRUC   
  37.         X       dw  0
  38.         Y       dw  0
  39.         Z       dw  0
  40.         OldDi   dw  0       ;where to erase last dot
  41.         Color   db  0       ;BASE color. a number 0-16 is added to it
  42.     Star_Struc      ENDS
  43.  
  44.     StarStrucSize = 9       ;number of bytes per entry
  45.  
  46. ;=== DATA
  47.  
  48. ScreenWidth EQU 320
  49. ScreenHeight EQU 200
  50.  
  51. NumRnds     EQU 400     ;number of random numbers defined
  52.  
  53. MaxZpos     EQU 4096
  54. MinZpos     EQU 2
  55. MaxStars    EQU 190
  56. NumColors   EQU 5       ;number of Base colors in the Color Chart
  57.  
  58. WarpSpeed   dw  15      ;how quickly the stars move toward ya
  59. MaxWarp     EQU 90
  60.  
  61. Xindex      dw  30      ;index into the StarRnd chart for X & Y
  62. Yindex      dw  230     ; -note they must be different; set em the same to
  63.                         ;see why
  64. Cindex      dw  0       ;index into ColorChart
  65.  
  66. ColorChart  db  0,16,32,48,64,80    ;a list of base colors (-1)
  67.  
  68. Stars       Star_Struc MaxStars DUP (<>) ;where all the data is held
  69. NumActive   dw  0       ;number of stars active
  70.  
  71. Palette     db  3 dup (0)   ;the palette.. first entrie is BG color (black)
  72.     i = 15
  73.     REPT    16
  74.             db  2*i,3*i,4*i
  75.         i=i-1
  76.     ENDM
  77.     i = 15
  78.     REPT    16
  79.             db  2*i,2*i,4*i
  80.         i=i-1
  81.     ENDM
  82.     i = 15
  83.     REPT    16
  84.             db  3*i,3*i,4*i
  85.         i=i-1
  86.     ENDM
  87.     i = 15
  88.     REPT    16
  89.             db  3*i,2*i,4*i
  90.         i=i-1
  91.     ENDM
  92.     i = 15
  93.     REPT    16
  94.             db  3*i,3*i,3*i
  95.         i=i-1
  96.     ENDM
  97.     i = 15
  98.     REPT    16
  99.             db  2*i,4*i,3*i
  100.         i=i-1
  101.     ENDM
  102.  
  103. ;=== Code Includes
  104. ;=== SUBROUTINES
  105.  
  106.     ;finds 1st available slot for a star and puts it there
  107. MakeStar PROC NEAR
  108.     pusha
  109.     mov     ax,cs
  110.     mov     es,ax
  111.     mov     ds,ax
  112.  
  113.     cmp     [NumActive],MaxStars    ;is there room for another star?
  114.     jae     NoEmptySpace            
  115.  
  116.     ;search for 1st available slot
  117.  
  118.     mov     si,0
  119. TryAgain:
  120.     cmp     word ptr [Stars.Z+si],0     ;is this slot empty?
  121.     je      GotOne                      ;yes, go fill it
  122.  
  123.     add     si,StarStrucSize
  124.     cmp     si,MaxStars*StarStrucSize
  125.     jb      TryAgain
  126.     jmp     NoEmptySpace
  127.  
  128. GotOne:         ;si points to the record for the star to fill
  129.     mov     di,[Yindex]         ;grab index for Ypos
  130.     add     di,di               ;multiply by 2 to make it a WORD index
  131.     mov     ax,[StarRnd+di]     ;get the number
  132.     shl     ax,3                ;multiply by 8- could been done in BAS file
  133.     mov     [Stars.Y+si],ax     ;and save off the number
  134.     
  135.     mov     di,[Xindex]         ;grab index for Xpos
  136.     add     di,di               ;... same as above, but for Xpos
  137.     mov     ax,[StarRnd+di]
  138.     shl     ax,3
  139.     mov     [Stars.X+si],ax
  140.  
  141.     mov     [Stars.Z+si],MaxZpos    ;reset Zpos to the max
  142.     inc     [NumActive]             ;we added a star so increase the counter
  143.  
  144.     mov     di,[Cindex]             ;grab the color index
  145.     mov     al,[ColorChart+di]      ;grab the BaseColor for the star
  146.     mov     [Stars.Color+si],al     ;save it in the record
  147.  
  148.     ;increase all the index pointers
  149.  
  150.     inc     [Cindex]                ;increases the color counter
  151.     cmp     [Cindex],NumColors
  152.     jb      OkColor
  153.     mov     [Cindex],0
  154. OkColor:
  155.     inc     [Yindex]                ;increases Yindex
  156.     cmp     [Yindex],NumRnds        ;note that for this one we
  157.     jb      YindNotZero             ; subtract NumRnds from Yindex if we
  158.     sub     [Yindex],NumRnds        ; go off the end of the chart
  159. YindNotZero:
  160.     inc     [Xindex]                ;increase Xindex
  161.     cmp     [Xindex],NumRnds        ;have we gone through the entire chart?
  162.     jb      XindNotZero             ;nope...
  163.  
  164. ;This clever bit of code makes more use out of the chart by increasing Yindex
  165. ; one additional unit each time Xindex goes through the entire chart... the
  166. ; result is nearly NumRND^2 random non-repeating points
  167.         
  168.     inc     [Yindex]                ;yes, so change Yindex so that we get a
  169.     mov     ax,[Yindex]             ;new set of random (x,y)
  170.     cmp     ax,[Xindex]             ;does Xindex = Yindex?
  171.     jne     NotTheSame              ;if the index were the same, you'd see 
  172.                                     ;a graph of the line Y = X, not good...
  173.     inc     [Yindex]                ;if they are the same, inc Yindex again
  174. NotTheSame:
  175.     mov     [Xindex],0              ;reset Xindex to 0
  176. XindNotZero:                        ;all done making the star...
  177.  
  178. NoEmptySpace:
  179.     popa
  180.     ret
  181. MakeStar ENDP
  182.  
  183. DisplayStars PROC NEAR
  184.     pusha
  185.     mov     ax,cs
  186.     mov     ds,ax
  187.     mov     ax,0a000h
  188.     mov     es,ax
  189.  
  190.     mov     si,0
  191. DispLoop:
  192.     mov     cx,[Stars.Z+si]
  193.     or      cx,cx                   ;if Zpos = 0 then this star is dead...
  194.     je      Cont                    ;continue to the next one- skip this one
  195.  
  196.     mov     di,[Stars.OldDi+si]     ;grab old Di
  197.     mov     byte ptr es:[di],0      ;erase the star
  198.     
  199.     cmp     cx,MinZpos
  200.     jl      TermStar                ;if Zpos < MinZpos then kill the star
  201.  
  202.     mov     ax,[Stars.Y+si]
  203.     movsx   dx,ah                   ;'multiply' Ypos by 256
  204.     shl     ax,8
  205.     
  206.     idiv    cx                      ;and divide by Zpos
  207.     add     ax,ScreenHeight/2       ;center it on the screen
  208.     mov     di,ax
  209.     cmp     di,ScreenHeight         ;see if the star is in range. 
  210.     jae     PreTermStar             ; If not, kill it
  211.     imul    di,ScreenWidth          ; DI = Y*ScreenWidth
  212.  
  213.     mov     ax,[Stars.X+si]
  214.     movsx   dx,ah                   ;multiply Xpos by 256
  215.     shl     ax,8
  216.  
  217.     idiv    cx                      ;and divide by Zpos
  218.     add     ax,ScreenWidth/2        ;center it on the screen
  219.     cmp     ax,ScreenWidth          ;are we inside the screen boundries?
  220.     jae     PreTermStar
  221.     add     di,ax                   ; DI = Y * ScreenWidth + X
  222.  
  223.     mov     [Stars.OldDi+si],di     ;save old di
  224.  
  225.     ;calculate the color below
  226.  
  227.     add     ch,cs:[Stars.Color+si]  ;i'm dividing cx (the zpos) by 256 and
  228.                                     ; putting the result in ch and adding
  229.                                     ; the base color to it in one instruction
  230.     mov     es:[di],ch              ;put the dot on the screen
  231.  
  232.     mov     ax,cs:[WarpSpeed]
  233.     sub     cs:[Stars.Z+si],ax      ;move the stars inward at WarpSpeed
  234.  
  235. Cont:
  236.     add     si,StarStrucSize        ;point to next record
  237.     cmp     si,MaxStars*StarStrucSize   ;are we done yet?
  238.     jb      DispLoop
  239.     popa
  240.     ret
  241.  
  242. PreTermStar:
  243.     mov     [Stars.Z+si],1  ;this is here so that the star will get erased
  244.     jmp     short Cont      ;next time through if I just went off and killed
  245.                             ;the star, it would leave a dot on the screen
  246. TermStar:
  247.     mov     [Stars.Z+si],0  ;this actually kills the star, after it has
  248.     dec     [NumActive]     ;been erased
  249.     jmp     short Cont
  250.  
  251. DisplayStars ENDP
  252.  
  253. ;=== CODE
  254.  
  255. START:
  256.     mov     ax,cs
  257.     mov     ds,ax
  258.     mov     es,ax
  259.  
  260.     mov     ax,0013h                ;set vid mode 320x200x256 graph
  261.     int     10h
  262.     
  263.     mov     dx,offset Palette
  264.     mov     ax,1012h                ; WRITE palette 
  265.     mov     bx,0                    
  266.     mov     cx,256                  ;write entire palette
  267.     int     10h                     ;doesn't matter if we didnt define it all
  268.  
  269. StarLoop:
  270.     call    MakeStar        ;make stars 2x as thick
  271.     call    MakeStar
  272.  
  273.     mov     dx,3dah
  274. VRT:
  275.     in      al,dx
  276.     test    al,8
  277.     jnz     VRT             ;wait until Verticle Retrace starts
  278.  
  279. NoVRT:
  280.     in      al,dx
  281.     test    al,8
  282.     jz      NoVRT           ;wait until Verticle Retrace Ends
  283.  
  284.     call    DisplayStars
  285.  
  286.     mov     ah,1            ;check to see if a char is ready
  287.     int     16h
  288.     jz      StarLoop        ;nope, continue
  289.     
  290.     mov     ah,0
  291.     int     16h             ;get the character & put in AX
  292.  
  293.     cmp     al,"+"          ;compare ASCII part (al) to see what was pressed
  294.     jne     NotPlus
  295.  
  296.     inc     [WarpSpeed]
  297.     cmp     [WarpSpeed],MaxWarp
  298.     jbe     StarLoop
  299.  
  300.     mov     [WarpSpeed],MaxWarp
  301.     jmp     StarLoop
  302.  
  303. NotPlus:
  304.     cmp     al,"-"
  305.     jne     NotMinus
  306.  
  307.     dec     [WarpSpeed]
  308.     cmp     [WarpSpeed],0
  309.     jge     StarLoop
  310.  
  311.     mov     [WarpSpeed],0
  312.     Jmp     StarLoop
  313.  
  314. NotMinus:
  315.  
  316.     mov     ax,0003h        ;set 80x25x16 char mode
  317.     int     10h
  318.     mov     ax,4c00h        ;return control to DOS
  319.     int     21h
  320. END START
  321.